package com.gt.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gt.context.BaseContext;
import com.gt.mapper.NoticeMapper;
import com.gt.pojo.PageResult;
import com.gt.pojo.dto.NoticeDTO;
import com.gt.pojo.dto.NoticePageQueryDTO;
import com.gt.pojo.entity.Notice;
import com.gt.pojo.vo.NoticeVO;
import com.gt.service.NoticeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public void add(NoticeDTO noticeDTO) {
        Notice notice =new Notice();
        BeanUtils.copyProperties(noticeDTO,notice);
        notice.setCreateTime(LocalDateTime.now());
        notice.setUpdateTime(LocalDateTime.now());
        noticeMapper.add(notice);
    }

    @Override
    public PageResult pageQuery(NoticePageQueryDTO noticePageQueryDTO) {
        PageHelper.startPage(noticePageQueryDTO.getPage(), noticePageQueryDTO.getPageSize());
        Page<Notice> page = noticeMapper.pageQuery(noticePageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        noticeMapper.deleteBatch(ids);
    }

    @Override
    public NoticeVO getById(Long id) {
        Notice notice = noticeMapper.getById(id);
        NoticeVO noticeVO = new NoticeVO();
        BeanUtils.copyProperties(notice,noticeVO);
        return noticeVO;
    }

    @Override
    public void update(NoticeDTO noticeDTO) {
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeDTO,notice);
        notice.setUpdateTime(LocalDateTime.now());
        noticeMapper.update(notice);
    }

    @Override
    public void deleteById(Long id) {
        noticeMapper.deleteById(id);
    }
}
